// 文件：TopNavigationBar.java
package frontend.user;

import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

public class TopNavigationBar extends JPanel {
    private final String[] categories;

    public TopNavigationBar(String[] categories) {
        this.categories = categories;
        initialize();
    }

    private void initialize() {
        setLayout(new BorderLayout(20, 0));
        setBackground(new Color(245, 245, 245));
        setBorder(BorderFactory.createEmptyBorder(15, 10, 0, 10));

        // 标签面板
        JPanel labelPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 10, 0));
        labelPanel.setOpaque(true);

        for (String category : categories) {
            JLabel label = new JLabel(category);
            label.setFont(new Font("微软雅黑", Font.BOLD, 15));
            label.setOpaque(true);
            label.setBackground(new Color(245, 245, 245));
            label.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

            // 鼠标点击事件
            label.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    if (tabSelectedListener != null) {
                        tabSelectedListener.onTabSelected(label.getText());
                    }

                    // 更新选中样式
                    for (Component comp : labelPanel.getComponents()) {
                        if (comp instanceof JLabel) {
                            ((JLabel) comp).setBackground(new Color(245, 245, 245));
                        }
                    }
                    label.setBackground(Color.WHITE);
                }
            });

            labelPanel.add(label);
        }

        add(labelPanel, BorderLayout.CENTER);
    }
    public interface OnTabSelectedListener {
        void onTabSelected(String category);
    }
    private OnTabSelectedListener tabSelectedListener;
    public void setOnTabSelectedListener(OnTabSelectedListener listener) {
        this.tabSelectedListener = listener;
    }
}
